package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.BizZichanExport;
import com.ruoyi.system.service.ISysDictDataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BizZichan;
import com.ruoyi.system.service.IBizZichanService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 资产管理Controller
 * 
 * @author ruoyi
 * @date 2024-12-03
 */
@Controller
@RequestMapping("/system/zichan")
public class BizZichanController extends BaseController
{
    private String prefix = "system/zichan";

    @Autowired
    private IBizZichanService bizZichanService;

    @Autowired
    private ISysDictDataService iSysDictDataService;

    @RequiresPermissions("system:zichan:view")
    @GetMapping()
    public String zichan()
    {
        return prefix + "/zichan";
    }

    /**
     * 查询资产管理列表
     */
    @RequiresPermissions("system:zichan:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BizZichan bizZichan)
    {
        startPage();
        List<BizZichan> list = bizZichanService.selectBizZichanList(bizZichan);
        return getDataTable(list);
    }

    /**
     * 导出资产管理列表
     */
    @RequiresPermissions("system:zichan:export")
    @Log(title = "资产管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BizZichan bizZichan)
    {
        List<BizZichan> list = bizZichanService.selectBizZichanList(bizZichan);
        List<BizZichanExport> listExport= new ArrayList<>();
         Integer j=1;
         for(BizZichan item:list){
             BizZichanExport bizZichanExport=new BizZichanExport();
             BeanUtils.copyProperties( item,bizZichanExport);
             bizZichanExport.setSn(j);
             //教育分类
             bizZichanExport.setJiaoyufenleimingcheng(iSysDictDataService.selectDictLabel("biz_jiaoyufenlei",item.getJiaoyufenlei()));
             //使用人
             bizZichanExport.setShiyongrenmingcheng(iSysDictDataService.selectDictLabel("biz_caiwufuzeren",item.getShiyongren()));
             //存放地点
             bizZichanExport.setCunfangdidianmingcheng(iSysDictDataService.selectDictLabel("biz_cunfangdidian",item.getCunfangdidian()));
             listExport.add(bizZichanExport);
             //更新rifd打印数量
             bizZichanService.updateZichanRifdShuliang(item.getZichanbianhao());
             j++;
         }
        ExcelUtil<BizZichanExport> util = new ExcelUtil<BizZichanExport>(BizZichanExport.class);
        return util.exportExcel(listExport, "打印条码数据");
    }

    @Log(title = "资产管理", businessType = BusinessType.EXPORT)
    @PostMapping("/exportZichan")
    @ResponseBody
    public AjaxResult exportZichan(BizZichan bizZichan)
    {
        List<BizZichan> list = bizZichanService.selectBizZichanList(bizZichan);
//
        Integer j=1;
        for(BizZichan item:list){

            //教育分类
            item.setJiaoyufenlei(iSysDictDataService.selectDictLabel("biz_jiaoyufenlei",item.getJiaoyufenlei()));
            //使用人
            item.setShiyongren(iSysDictDataService.selectDictLabel("biz_caiwufuzeren",item.getShiyongren()));
            //存放地点
            item.setCunfangdidian(iSysDictDataService.selectDictLabel("biz_cunfangdidian",item.getCunfangdidian()));
            item.setCaizhengfenlei(iSysDictDataService.selectDictLabel("biz_caizhengfenlei",item.getCaizhengfenlei()));
//使用部门
            item.setShiyongbumen(iSysDictDataService.selectDictLabel("biz_shiyongbumen",item.getShiyongbumen()));
            //管理人
            item.setGuanliren(iSysDictDataService.selectDictLabel("biz_caiwufuzeren",item.getGuanliren()));

            item.setGuanlibumen(iSysDictDataService.selectDictLabel("biz_shiyongbumen",item.getGuanlibumen()));

            j++;
        }
        ExcelUtil<BizZichan> util = new ExcelUtil<BizZichan>(BizZichan.class);
        return util.exportExcel(list, "资产数据");
    }


    /**
     * 新增资产管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存资产管理
     */
    @RequiresPermissions("system:zichan:add")
    @Log(title = "资产管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BizZichan bizZichan)
    {
        BizZichan z=bizZichanService.selectBizZichanByBianhao(bizZichan.getZichanbianhao());
        if(z!=null){
            return AjaxResult.error("资产编号已经存在");
        }
        return toAjax(bizZichanService.insertBizZichan(bizZichan));
    }

    /**
     * 修改资产管理
     */
    @RequiresPermissions("system:zichan:edit")
    @GetMapping("/edit/{sn}")
    public String edit(@PathVariable("sn") Long sn, ModelMap mmap)
    {
        BizZichan bizZichan = bizZichanService.selectBizZichanBySn(sn);
        mmap.put("bizZichan", bizZichan);
        return prefix + "/edit";
    }

    /**
     * 修改保存资产管理
     */
    @RequiresPermissions("system:zichan:edit")
    @Log(title = "资产管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BizZichan bizZichan)
    {
        return toAjax(bizZichanService.updateBizZichan(bizZichan));
    }

    /**
     * 删除资产管理
     */
    @RequiresPermissions("system:zichan:remove")
    @Log(title = "资产管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bizZichanService.deleteBizZichanBySns(ids));
    }

    @Log(title = "资产管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("system:zichan:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BizZichan> util = new ExcelUtil<BizZichan>(BizZichan.class);
        List<BizZichan> zichanList = util.importExcel(file.getInputStream());
        String message = bizZichanService.importZichan(zichanList);

        return AjaxResult.success(message);
    }


    @GetMapping("/huizong")
    @ResponseBody
    public AjaxResult huizong()
    {
        return AjaxResult.success(bizZichanService.selectZichanHuizong());
    }

}
